package com.dzx.permissionmanagersystem.util;

import com.dzx.permissionmanagersystem.exception.ParamException;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.MapUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.*;

/**
 * @author DuanZhaoXu
 * @ClassName:
 * @Description:
 * @date 2018年11月29日 10:03:20
 */
public class BeanValidator {

    private  static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();

    public  static  <T>Map<String,String> validate(T t,Class... groups){
        Validator validator = validatorFactory.getValidator();
        Set valSet =  validator.validate(t,groups);
        if(valSet.isEmpty()){
          return Collections.emptyMap();
        }else {
            LinkedHashMap<String, String> errors = Maps.newLinkedHashMap();
            Iterator iterator = valSet.iterator();
            iterator.forEachRemaining(o -> {
                ConstraintViolation constraintViolation = (ConstraintViolation) o;
                errors.put(constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage());

            });
            return errors;
        }
    }


    public  static  Map<String,String> validateList(Collection<?> collection){
        Preconditions.checkNotNull(collection);

        Iterator  iterator = collection.iterator();
       Map errors;
       do{

           if(!iterator.hasNext()){
               return Collections.emptyMap();
           }
           Object object = iterator.next();
           errors =  validate(object,new Class[0]);
       }while (errors.isEmpty());
       return  errors;
    }


    public  static  Map<String,String> validateObject(Object first,Object... objects){
         if(objects!=null && objects.length>0){
             return  validateList(Lists.asList(first,objects));
         }else{
             return validate(first,new Class[0]);
         }
    }


    public  static  void  check(Object param) throws ParamException {
         Map<String,String> map = BeanValidator.validateObject(param);
        if(MapUtils.isNotEmpty(map)){
            throw  new ParamException(map.toString());
        }

    }


}
